<?php
/****
* 活动列表模块, 处理 活动的搜索\排序 等相关
****/

import('ORG.Util.Page');
import('@.Model.Where.EventWhereMachine');
class BrowseAction extends InitAction{
    static $COUNT_PER_PAGE=10;
    protected function _initialize(){
        parent::_initialize();
    }
    public function index(){
        /*
        //TODO 获取用户收藏信息
        $uid= $this->isLogged();
        if (false===$uid){
            $uid=null;
        }
        //$em= new EventModel('', $uid);
        */

        $em= new EventModel();
	$something = $_GET['args'];
	if($something == "http://www.qsc.zju.edu.cn/apps/notice/index.php")
		$something = "http://www.qsc.zju.edu.cn/apps/notice/index.php/browse/index/type/1";
        $map= $this->parseURL($something);
        
        $this->assign('role', $_GET['role']);
        
        $this->assign('types', EventModel::getTypes());
        $this->assign('tags', $this->getHotTag());
        $this->assign('groups', $this->getHotGroup());
        
        $map->state(EventModel::C('STATE_NORMAL'));
        //$map->isEnded(false);
        //分页
        import("@.Common.NoticePage");
        $count=$em->where($map->get())->count();
        $page= new NoticePage($count, self::$COUNT_PER_PAGE);
        $show= $page->show();
        $this->assign('page', $show);
        
        $events =$em->where($map->get())->limit($page->firstRow.','.$page->listRows)->order('time_start ASC')->select();
        $this->assign('events', $events);
        
        $this->display('index');
    }
    /****
    * 获取最近热门tag
    ****/
    protected function getHotTag(){
        $tags= S('hotTags');        //数据查询较复杂, 启用缓存
        if (false==$tags ||C('APP_DEBUG')){
            $etm= new EventTagModel();
            $tags= $etm->listHotTag(20);
            S('hotTags', $tags, 3600);
        }
        return $tags;
    }
    /****
    * 获取活跃社团账户列表
    */
    protected function getHotGroup(){
        $groups= S('hotGroups');
        if (!$groups ||C('APP_DEBUG')){
            $am= new AccountModel();
            $map=array(
                'role'=>AccountModel::C('ROLE_GROUP'),
            );
            $groups= $am->where($map)->order('`uid` DESC')->limit(12)->select();
            S('hotGroups', $groups, 3600);
        }
        return $groups;
    }
    /****
    * 解析browse复杂的url
    * url示例 /group/week/type/1
    *         /user/2011/10/?type=2
    */
    protected function parseURL($args){
        static $ARGS_NAME ='';
        $ARGS_NAME= implode('|', array('type', 'tag', 'q'));
        $map= new EventWhereMachine();
        
        //活动发布者
        if (false!==stripos($args, '/group')){
            $_GET['role']= 'group';
        } else if (false!==stripos($args, '/user')){
            $_GET['role']= 'user';
        } else{
            $_GET['role']= null;
        }
        $map->role($_GET['role']);
        //活动日期
        $s= getdate();  $t=$s;  
        $_GET['date']= true;    //单纯为了页面中setTab使用
        if (false!==stripos($args, '/week')){
            $t['mday']+=7;
            $map->timeRange($s, $t);
        } else if(false!==stripos($args, '/month')){
            $t['mon']++;
            $map->timeRange($s, $t);
        } else if (false!==stripos($args, '/ended')){
            $map->isEnded(true);
        } else if( preg_match('/(?!type|tag|q)\/(\d{4})(?:\/(\d{1,2}))?/i', $args, $parts)){
            $map->date($parts[1], $parts[2]);
        } else{
            $_GET['date']=null;
        }
        //搜索关键字
        if (preg_match("/\/q\/([^\/&]+)/i", $args, $parts)){
            $_GET['q']= $parts[1];
        }
        if ($_GET['q']) {
            $map->word($_GET['q']);
        }
        //类型
        if (preg_match("/\/type\/(\d+)/i", $args, $parts)){
            $map->type($parts[1]);
            $_GET['type']= $parts[1];
        }
        //tag
        if (preg_match("/\/tag\/([^\/&]+)/i", $args, $parts)){
            $map->tag($parts[1]);
            $_GET['tag']= $parts[1];
        }
        
        return $map;
    }
    public function t(){
        return $this->index();
    }
    public function type(){
        return $this->index();
    }
    public function q(){
        return $this->index();
    }
}
?>